﻿@using KRM.Core
@model IEnumerable<KRM.Core.vLoadItemDetail>
@{
    vLoadDetail load = ViewBag.Load;
    //var unplanload = load.LoadStatusID >= LoadStatus.BOOKEDIN.StatusId();
    //var pickOrder = true;
    //var trunked = true;
    //var atHub = true;
    //var confirmDelivery = true;
    //var despatchOrder = false;
    //if (load.Carrier != null && load.Carrier.IsHub == true && !load.Carrier.IsOwnHub)
    //{
    //    pickOrder = load.LoadStatus == "PLANNED";
    //    trunked = load.LoadStatus == LoadStatus.PICKING.ToString();
    //    atHub = load.LoadStatus == LoadStatus.TRUNKED.ToString();

    //    confirmDelivery = load.LoadStatus == "AT HUB";
    //}
    //else
    //{
    //    confirmDelivery = load.LoadStatus == "DESPATCHED";
    //    pickOrder = load.LoadStatus == "BOOKED IN";
    //    trunked = false;
    //    atHub = false;

    //}
    //var hasCancelledOrReplanned = (Model.IndexOf(li => li.ItemCancelled ||
    //                LP_LoadItemStatus.GetStatus(li.ItemStatusID.Value).StatusOrder < LpLoadItemStatusExtension.StatusOrder(LoadItemStatus.BOOKEDIN)) > -1);
    //if (load.LoadStatusID == LoadStatus.PICKING.StatusId() && !hasCancelledOrReplanned && (load.Carrier.IsHub == false || load.Carrier.IsOwnHub))
    //{
    //    despatchOrder = true;
    //}
}

<script>
    $(document).ready(function() {
        InitAjaxTable($(".loaditemdetailtable"));
        $('.datetimepicker').datetimepicker({
            language: 'en',
            pick12HourFormat: true
        });

        $("#fr-RemoveLoadItem").submit(function() {
            var serializedForm = $(this).serialize();
            App.blockUI($("#fr-RemoveLoadItem"));
            $.ajax({
                type: 'POST',
                url: '/load/RemoveLoadItem',
                data: serializedForm,
                success: function (data) {
                    alert("Success!");
                    listloaddetailselected(@load.LoadID);
                    $("#ChooseReason").modal("hide");
                    $(".email-content").html("");
                    App.unblockUI($("loaditemdetailtable"));
                    App.unblockUI($("#fr-RemoveLoadItem"));
                }
            });
            return false;
        });
    });
    
    function reasonemailchange(reasonid) {
        App.blockUI($("#fr-RemoveLoadItem"));
        if (reasonid == 12) {
            $(".deliverydate").show();
        } else {
            $(".deliverydate").hide();
        }
        $.ajax({            
            url: "/load/reasonemailchange?reasonid=" + reasonid,
            success:function(data) {
                $(".email-content").html(data);
                App.unblockUI($("#fr-RemoveLoadItem"));
            }
        });
    }
    
    //function pickselectedorder(loadid) {
    //    var despatchlabels = false;
    //    var createPackingListLabels = $("#despatchlabels").is(":checked");
    //    if (createPackingListLabels) {
    //        if (confirm("Create the labels back to front so the last label is at the top of the file.")) {
    //            despatchlabels = true;
    //        }
    //    }
    //    App.blockUI($(".listloaditemdetail"));
    //    var vehicleid = $("#VehicleID").val();
    //    var driverid = $("#DriverCheckID").val();
    //    var pickingsummary = $("#PickingSummary").is(":checked");
    //    var driverManifest = $("#DriverManifest").is(":checked");
    //    var packingList = $("#PackingList").is(":checked");
    //    var deliveryNotes = $("#DeliveryNotes").is(":checked");
    //    var boxLabels = $("#BoxLabels").is(":checked");
    //    var pickDetailSummary = $("#PickDetailSummary").is(":checked");
    //    var sendEmail = $("#SendEmail").is(":checked");
    //    var palletLabel = $("#PalletLabel").is(":checked");
    //    var returnsForm = $("#ReturnsForm").is(":checked");
    //    $.ajax({
    //        url: "/load/pickselectedorder",
    //        data:{loadid : loadid, despatchlabels : despatchlabels, vehicleid:vehicleid,driverid:driverid, createPackingListLabels:createPackingListLabels,
    //            pickingsummary:pickingsummary, driverManifest:driverManifest, packingList:packingList, deliveryNotes :deliveryNotes,
    //            boxLabels:boxLabels, pickDetailSummary:pickDetailSummary, sendEmail:sendEmail, palletLabel:palletLabel, returnsForm:returnsForm},
    //        success: function (data) {
    //            App.unblockUI($(".listloaditemdetail"));
    //            if (data.success == "false") {
    //                alert(data.message);
    //            } else {
    //                alert(data.message);
    //                if (data.button == "Trunked") {
    //                    $(".btnTrunked").removeAttr("disabled");
    //                }else if (data.button == "DespatchOrder") {
    //                    $(".btnDespatchOrder").removeAttr("disabled");
    //                }
    //            }
    //            $(".btnPickOrder").attr("disabled", "disabled");
    //            $.ajax({
    //                url: "/load/updatesummary",
    //                success: function(data) {
    //                    $(".TotalUnplannedLoad").html(data.TotalUnplannedLoad);
    //                    $(".TotalReplanLoads").html(data.TotalReplanLoads);
    //                    $(".TotalPlannedLoads").html(data.TotalPlannedLoads);
    //                    $(".TotalBookedInLoads").html(data.TotalBookedInLoads);
    //                    $(".TotalPickedLoads").html(data.TotalPickedLoads);
    //                    $(".TotalDespatchedLoads").html(data.TotalDespatchedLoads);
    //                }
    //            });
    //        }, error:function() {
    //            alert("Error!");
    //            App.unblockUI($(".listloaditemdetail"));
    //        }
    //    });
    //}
    
    //function despatchorder(loadid) {
    //    App.blockUI($(".listloaditemdetail"));
    //    var vehicleid = $("#VehicleID").val();
    //    var driverid = $("#DriverCheckID").val();
    //    var deliveryNotes = $("#DeliveryNotes").is(":checked");
    //    var sendEmail = $("#SendEmail").is(":checked");
    //    $.ajax({
    //        url: "/load/despatchorder",
    //        data: {
    //            loadid: loadid,
    //            vehicleid: vehicleid,
    //            driverid: driverid,
    //            deliveryNotes: deliveryNotes,
    //            sendEmail: sendEmail
    //        },
    //        success: function(data) {
    //            App.unblockUI($(".listloaditemdetail"));
    //            if (data.success == "false") {
    //                alert(data.message);
    //            } else {
    //                alert(data.message);
    //                viewloadrefreshsummary();
    //            }
    //            $(".btnDespatchOrder").attr("disabled", "disabled");
    //        }, error:function() {
    //            alert("Error!");
    //            App.unblockUI($(".listloaditemdetail"));
    //        }
    //    });
    //}
    
    //function trunked(loadid) {
    //    App.blockUI($(".listloaditemdetail"));
    //    $.ajax({
    //        url: "/load/trunked",
    //        data: {loadid: loadid},
    //        success: function(data) {
    //            App.unblockUI($(".listloaditemdetail"));
    //            if (data.success == "false") {
    //                alert(data.message);
    //            } else {
    //                alert(data.message);
    //            }
    //            $(".btnTrunked").attr("disabled", "disabled");
    //            $(".btnAtHub").removeAttr("disabled");
    //        }, error:function() {
    //            alert("Error!");
    //            App.unblockUI($(".listloaditemdetail"));
    //        }
    //    });
    //}
    
    //function athub(loadid) {
    //    App.blockUI($(".listloaditemdetail"));
    //    $.ajax({
    //        url: "/load/athub",
    //        data: {loadid: loadid},
    //        success: function(data) {
    //            App.unblockUI($(".listloaditemdetail"));
    //            if (data.success == "false") {
    //                alert(data.message);
    //            } else {
    //                alert(data.message);
    //            }
    //            $(".btnAtHub").attr("disabled", "disabled");
    //            $(".btnConfirmDelivery").removeAttr("disabled");
    //        }, error:function() {
    //            alert("Error!");
    //            App.unblockUI($(".listloaditemdetail"));
    //        }
    //    });
    //}
</script>

@Html.Partial("Partial/_MessageResult")

<div class="border-padding10">
    <div class="row-fluid">
        <div class="span2">
            <h3 style="margin: 0; line-height: normal">@load.LoadCode</h3>
        </div>
        <div class="span3">Total Packages: @Model.Sum(a => a.Packages)</div>
    </div>
    <div class="row-fluid">
        <div class="span2">
            Qty Item: @Model.Count()
        </div>
        <div class="span3">Total Weight: @Model.Sum(a => a.WeightKG)</div>
        <div class="span1">Vehicle</div>
        <div class="span3">
            @Html.DropDownList("VehicleID", null, new { @class = "input-block-level" })
        </div>
    </div>
    <div class="row-fluid">
        <div class="span2">
            Create By: @load.Username
        </div>
        <div class="span3">Vehicle Max Wgt: @load.VehicleMaxWeight</div>
        <div class="span1">Driver</div>
        <div class="span3">
            @Html.DropDownList("DriverCheckID", null, new { @class = "input-block-level" })
        </div>
        @*<div class="span4 text-right">
            <input type="button" class="btn btn-info" value="Generate Costs" />
            <input type="button" class="btn btn-info" value="Export to AutoRoute" />
        </div>*@
    </div>
</div>
<table class="table table-bordered table-fix krmtable loaditemdetailtable" data-height="200">
    <thead>
        <tr>
            <th></th>
            <th>Cancelled</th>
            <th>Delivered</th>
            <th>ItemStatus</th>
            <th>OrderRef</th>
            <th>PostCode</th>
            <th>OrderItemID</th>
            <th>Qty</th>
            <th>Product</th>
            <th>SpecialInstructions</th>
            <th>WeightKG</th>
            <th>Packages</th>
            <th>Customer</th>
            <th>Address</th>
            <th>Telephone1</th>
            <th>Telephone2</th>
            <th>EmailAddress</th>
            <th>ProductID</th>
            <th>ProductOptionID</th>
            <th>DatePlaced</th>
            <th>WarehouseID</th>
            <th>CarrierName</th>
            <th>CarrierID</th>
            <th>DropID</th>
            <th>OrderID</th>
            <th>LoadID</th>
            <th>ItemCancelled</th>
            <th>DropNumber</th>
            <th>DeliveryDate</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    <div class="btn-group">
                        <a href="#" data-toggle="dropdown" class="btn dropdown-toggle"><i class="icon-cog"></i><span class="caret"></span></a>
                        <ul class="dropdown-menu dropdowm-right">
                            <li><a href="javascript:;" onclick="removeloaditem(@item.LoadID,@item.OrderItemID)">Remove Item</a></li>
                            <li><a href="javascript:;" onclick="PopupCenter('/load/viewloaditemparts?loadId=@item.LoadID&orderItemId=@item.OrderItemID', 800, 600)">View Part</a></li>
                            <li><a href="/orderview/orderdetail?orderref=@item.OrderRef" target="_blank">View Order</a></li>
                            <li><a href="javascript:;" onclick="PopupCenter('/load/CommentsCons?loadId=@item.LoadID&orderItemId=@item.OrderItemID', 800, 600)">Comments / Cons</a></li>
                            <li><a href="javascript:;" onclick="PopupCenter('/orderview/orderitemphoto/@item.OrderItemID?orderref=@item.OrderRef', 900, 600)">Add Photos</a></li>
                            <li><a href="javascript:;" onclick="PopupCenter('/orderview/deliverydate?orderid=@item.OrderID', 1200, 600);">Select Date</a></li>
                        </ul>
                    </div>
                </td>
                <td></td>
                <td style="text-align: center">
                    <input type="checkbox" />
                </td>
                <td>@item.ItemStatus</td>
                <td>@item.OrderRef</td>
                <td>@item.Postcode</td>
                <td>@item.OrderItemID</td>
                <td>@item.Qty</td>
                <td>@item.Product</td>
                <td>@item.SpecialInstructions</td>
                <td>@item.WeightKG</td>
                <td>@item.Packages</td>
                <td>@item.Customer</td>
                <td>@item.Address</td>
                <td>@item.Telephone1</td>
                <td>@item.Telephone2</td>
                <td>@item.EmailAddress</td>
                <td>@item.ProductID</td>
                <td>@item.ProductOptionID</td>
                <td>@item.DatePlaced</td>
                <td>@item.WarehouseID</td>
                <td>@item.carrierName</td>
                <td>@item.carrierID</td>
                <td>@item.DropID</td>
                <td>@item.OrderID</td>
                <td>@item.LoadID</td>
                <td>@item.ItemCancelled</td>
                <td>@item.DropNumber</td>
                <td>@item.DeliveryDate</td>
            </tr>
        }
    </tbody>
</table>

<div id="ChooseReason" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="ChooseReason" aria-hidden="true">
    <form action="/load/RemoveLoadItem" class="margin0" id="fr-RemoveLoadItem" method="POST">
        <input type="hidden" name="loadid" value="@load.LoadID"/>
        <input type="hidden" name="orderitemid" id="hf-orderitemid"/>
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
            <h5>Choose Reason</h5>
        </div>
        <div class="modal-body">
            <div class="row-fluid">
                <div class="span3">Reason</div>
                <div class="span6">
                    @Html.DropDownList("ReasonID", null, "---Please choose---", new { @class = "input-block-level", onchange = "reasonemailchange(this.value)", required = "required" })
                </div>
            </div>
            <div class="row-fluid deliverydate" style="display: none">
                <div class="span3">Delivery Date</div>
                <div class="span6">
                    <div class="input-append datetimepicker">
                        <input data-format="dd/MM/yyyy" type="text" name="deliverydate" value="@string.Format("{0:dd/MM/yyyy}", DateTime.Now)">
                        <span class="add-on">
                            <i data-time-icon="icon-time" data-date-icon="icon-calendar" class="icon-calendar"></i>
                        </span>
                    </div>
                </div>
            </div>
            <div class="row-fluid">
                <p>Email content</p>
                <div class="email-content"></div>
            </div>
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary w100" type="submit">Send</button>
            <button class="btn w100" data-dismiss="modal" aria-hidden="true">Cancel</button>
        </div>
    </form>
</div>

@*<div class="row-fluid" style="margin-top: 10px">
    <div class="span2">
        <input type="button" class="btn btn-primary" value="Update Despatched" style="width: 100%; margin-bottom: 10px; height: 40px;"
            onclick="viewloadupdatedespatched()" />
        <input type="button" class="btn btn-primary" value="Unplan Load" style="width: 100%; height: 40px;" 
            @(!unplanload ? "disabled=disabled" : "")  onclick="unplanload(@load.LoadID)" />
    </div>
    <fieldset class="span5" style="margin-top: -7px">
        <legend>
            <label>
                <input type="checkbox" class="cbcheck" checked="checked" onclick="deliverydocumentchange($(this))" /> Delivery Documents</label></legend>
        <div class="row-fluid">
            <div class="span6">
                <label>
                    <input type="checkbox" class="cbcheck check5" checked="checked" id="PickingSummary" />
                    Picking Summary</label>
            </div>
            <div class="span6">
                <label>
                    <input type="checkbox" class="cbcheck check5" checked="checked" id="DriverManifest" />
                    Driver Manifest</label>
            </div>
        </div>
        <div class="row-fluid">
            <div class="span6">
                <label>
                    <input type="checkbox" class="cbcheck" id="PackingList" />
                    Packing List</label>
            </div>
            <div class="span6">
                <label>
                    <input type="checkbox" class="cbcheck check5" checked="checked" id="DeliveryNotes" />
                    Delivery Notes</label>
            </div>
        </div>
    </fieldset>
    <div class="span5">
        <div class="row-fluid">
            <div class="span6">
                <label>
                    <input type="checkbox" class="cbcheck check5" checked="checked" id="BoxLabels" />
                    Box Labels</label>
            </div>
            <div class="span6">
                <label>
                    <input type="checkbox" class="cbcheck" id="PickDetailSummary" />
                    Driver Load Acceptance Summary</label>
            </div>
        </div>
        <div class="row-fluid">
            <div class="span6">
                <label>
                    <input type="checkbox" class="cbcheck" id="DespatchLabels" />
                    Despatch Labels</label>
            </div>
            <div class="span6">
                <label>
                    <input type="checkbox" class="cbcheck" checked="checked" id="SendEmail" />
                    Send email</label>
            </div>
        </div>
        <div class="row-fluid">
            <div class="span6">
                <label>
                    <input type="checkbox" class="cbcheck" id="PalletLabel" />
                    Pallet Label</label>
            </div>
            <div class="span6">
                <label>
                    <input type="checkbox" class="cbcheck" checked="checked" id="ReturnsForm" />
                    Returns Form</label>
            </div>
        </div>
    </div>
</div>
<div class="row-fluid">
    <div class="span3">
        <button type="button" class="btn btn-primary btnPickOrder" style="width: 100%" @(pickOrder ? "" : "disabled=disabled") onclick="pickselectedorder(@load.LoadID)">1.<br />
            Pick selected order<br />
            (Generate Pick Notes)</button>
    </div>
    <div class="span3">
        <button type="button" class="btn btn-primary btnTrunked" style="width: 100%; margin-bottom: 10px" 
            @(trunked ? "" : "disabled=disabled") onclick="trunked(@load.LoadID)">1(a) Trunked</button>
        <button type="button" class="btn btn-primary btnAtHub" style="width: 100%" @(atHub ? "" : "disabled=disabled") onclick="athub(@load.LoadID)">1(b) At Hub</button>
    </div>
    <div class="span3">
        <button type="button" class="btn btn-primary btnDespatchOrder" onclick="despatchorder(@load.LoadID)" style="width: 100%" @(despatchOrder ? "" : "disabled=disabled")>2.<br />
            Despatch order<br />
            (Generate Delivery Note)</button>
    </div>
    <div class="span3">
        <button type="button" class="btn btn-primary btnConfirmDelivery" style="width: 100%; height: 70px;" @(confirmDelivery ? "" : "disabled=disabled")>3.<br />
            Confirm Delivery</button>
    </div>
</div>*@
